<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/4/14
 * Time: 10:31
 */

namespace Admin\Controller;
use User\Api\UserApi;

class StatisticsController extends AdminController
{

    public function  index(){
        $param=I('');
        if($param['month']){
            $start =$param['month']."-01";
            $end =$param['month']."-31";
        }else{
            $day = date('m-d', strtotime('-15 day',time()));
            $nowdate = date('m-d', time());
            $start=$day;
            $end=$nowdate;
        }

        $where['date'] = array('between',array($start,$end));
        $list = M('statistics')->where($where)->order('date asc')->select();
        //echo  M('statistics')->getLastSql();
        $date='';
        $nums='';
        $max=200;
        foreach ($list as $key=>&$li){
            $date .="'".$li["date"]."',";
            $nums .="'".$li["nums"]."',";
            if($li['nums']>$max){
                $max=$this->format_max($li['nums']);
            }
        }

        if(!$nums){
            $this->error('没有数据');
        }

        $this->assign('date',$date);
        $this->assign('nums',$nums);
        $this->assign('max',$max);
        $this->display();
    }

    /**
     * 用户统计
     */
    public function user_statistics(){
        $map ='';
        $p=I('p');
        if($p){
            $p=$p-1;
            $p=$p*10;
        }else{
            $p=0;
        }
        $param=I('');
        $begin_time=strtotime(I('begin_time'));
        if(I('end_time')){
            $end_time = strtotime('+1 day', strtotime(I('end_time')));
        }
        if(!$begin_time&&!$end_time){
            $param['begin_time'] = date('Y-m-d', time());
            $param['end_time']   = date('Y-m-d', time());
        }
        //$map["FROM_UNIXTIME(uptime, '%Y-%m-%d')"] =date('Y-m-d', time());
        if($begin_time &&$end_time){
            $map2['createtime'] = array('between',array($begin_time,$end_time));
        }else{
            $map2 = "FROM_UNIXTIME(createtime, '%Y-%m-%d')='" . date('Y-m-d', time()) . "'";
        }
        if($param['uid']){
            $map['uid'] = $param['uid'];
        }

        //$map['is_robot'] = 0;
        $map['type'] = 2;
        $lists = M('user_trade')->where($map)->where($map2)->field('uid,SUM(money) as totalmoney')->order('createtime desc')->limit($p,20)->group('uid')->select();
    // echo  M('user_trade')->getLastSql();
        $total_yl=0;
        foreach ($lists as &$li){
            $where='';
            $where['uid']  = $li['uid'];
            $where['type'] = 3;
            $totalmoney = $li['totalmoney']?$li['totalmoney']:0;
            if($begin_time &&$end_time){
                $where2['createtime'] = array('between',array($begin_time,$end_time));
            }else {
                $where2 = "FROM_UNIXTIME(createtime, '%Y-%m-%d')='" . date('Y-m-d', time()) . "'";
            }
            $win=M('user_trade')->where($where)->where($where2)->field('sum(money) as money')->find();
          
             $win_money= $win['money']?$win['money']:0;
          
			$yingli = $li['totalmoney']-$win['money'];

            $total_yl = $total_yl+$yingli;
            if($yingli <0){
                $yingli = abs($yingli);
                $yingli =  "-".$this->format_money($yingli);
            }else{
                $yingli = $this->format_money($yingli);
            }
            $li['yingli'] = $yingli;

            $li['nickname'] = M('member')->where('uid='.$li['uid'])->getField('nickname');

            $li['totalmoney'] = $this->format_money($totalmoney);
  			$li['win_money']= $this->format_money($win_money);
            //echo M('user_trade')->getLastSql();
            //var_dump($win);
        }
       // var_dump($total_yl);

        $num=M('user_trade')->where($map)->where($map2)->group('uid')->select();

        //今日平台总盈利里统计
        $map3 = "FROM_UNIXTIME(createtime, '%Y-%m-%d')='" . date('Y-m-d', time()) . "'";
        $map4['type'] = 2;
        $list_cnt=M('user_trade')->field('uid,SUM(money) as totalmoney')->where($map4)->where($map3)->select();
        //echo M('user_trade')->getLastSql();exit;
        $yingli_cnt=0;
        foreach ($list_cnt as $ls){
            $yl_where['type'] = 3;
            $yl_where2 = "FROM_UNIXTIME(createtime, '%Y-%m-%d')='" . date('Y-m-d', time()) . "'";
            $yl_where['is_robot'] =0;
            $win=M('user_trade')->where($yl_where)->where($yl_where2)->field('sum(money) as money')->find();
            $win_money= $win['money']?$win['money']:0;
            $yingli_cnt = $ls['totalmoney']-$win_money;
        }
        //$yingli_cnt=$this->format_money($yingli_cnt);
        if($yingli_cnt <0){
            $yingli_cnt = abs($yingli_cnt);
            $yingli_cnt =  "-".$this->format_money($yingli_cnt);
        }else{
            $yingli_cnt = $this->format_money($yingli_cnt);
        }
        //总盈利
        $map5['type'] = 2;
        $list_total_cnt=M('user_trade')->field('uid,SUM(money) as totalmoney')->where($map5)->select();
        //echo M('user_trade')->getLastSql();exit;
        $yingli_total_cnt=0;
        foreach ($list_total_cnt as $ls){
            $yl_where['type'] = 3;
            //$yl_where2 = "FROM_UNIXTIME(createtime, '%Y-%m-%d')='" . date('Y-m-d', time()) . "'";
            $yl_where['is_robot'] =0;
            $win=M('user_trade')->where($yl_where)->field('sum(money) as money')->find();
            $win_money= $win['money']?$win['money']:0;
            $yingli_total_cnt = $ls['totalmoney']-$win_money;
        }

        //$yingli_cnt=$this->format_money($yingli_cnt);
        if($yingli_cnt <0){
            $yingli_total_cnt = abs($yingli_total_cnt);
            $yingli_total_cnt =  "-".$this->format_money($yingli_total_cnt);
        }else{
            $yingli_total_cnt = $this->format_money($yingli_total_cnt);
        }

        $nums=count($num);
        $this->setPage($nums);
        $this->assign('param',$param);
        $this->assign('yingli_cnt',$yingli_cnt);
        $this->assign('yingli_total_cnt',$yingli_total_cnt);
        $this->assign('_list',$lists);
        $this->display();
    }

    /**
     * 格式化金额
     * @param $money
     */
    function format_money($money){
        if ($money >= 10000) {
            $money = ($money / 10000) . '万';
        } else {
            $money = $money;
        }
        return $money?$money:0;
    }

    function  format_max($max){
        $num =  strlen(floor($max));
        if($num ==2){
            $wei = 100;
        }else if($num ==3){
            $wei = 100;
        }else if($num ==4){
            $wei = 1000;
        }else if($num ==5){
            $wei = 10000;
        }else if($num ==6){
            $wei = 100000;
        }
        $frist = substr($max, 0, 1);
        $max=$frist*$wei+$wei;
        return $max;
    }

}